# -*- coding: utf-8 -*-

from flask import Flask, url_for, render_template, redirect, request, jsonify
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from entity import Questnr, Option
import json
from bl import bl
app = Flask(__name__)

engine = create_engine('mysql+pymysql://root:yebu666888@127.0.0.1:3306/questionnaire_survey?charset=utf8')
DBSession = sessionmaker(bind=engine)

# @app.route('/questnr/')
@app.route('/questnr/<questnr_id>')
def questnr_by_id(questnr_id=None):
    session = DBSession()

    questnr = session.query(Questnr, Option).join(Option, Option.questnr_id == Questnr.questnr_id).filter(
        Questnr.questnr_id == questnr_id).all()
    session.close()
    return render_template('questnr.html', questnr=questnr)

#url_for('static', filename='style.css')


@app.route('/receive_answer',methods=["POST"])
def answer_by_id():
    option_list = request.json
    # option_list=request.get_json("http://127.0.0.1:800/questnr/12",'option_list')
   # return {"questnr.html":request.form.get('option_list')}
    ol = []
    for q_id in option_list.keys():
        ol.append({'questionId':int(q_id), 'answer':option_list[q_id]})
    d={}
    d['answerList'] = ol
    d['customerInfo'] = {
            "name": u"吕布",
            "contactNumber": "13000000000",
            "credentialCategory": "1",
            "credentialNumber": "342630190012123434"
        }
    d['assetsList'] = [
        {
            "assetsName": u"xx资产1",
            "priceList": [
                {"dt": "20170414", "price": "10000.11"},
                {"dt": "20170514", "price": "10000.11"},
                {"dt": "20170813", "price": "20000.11"}
            ]
        },

        {
            "assetsName": u"yy资产2",
            "priceList": [
                {"dt": "20170414", "price": "40000.11"},
                {"dt": "20170514", "price": "50000.11"},
                {"dt": "20170813", "price": "70000.11"}
            ]
        }
    ]

    out = bl(d)
    return redirect(render_template('survey_result.html', assetsList=out))
    #
    # def result():
    #     print request.json
    #     result={request.json['assetsName','dt','proportion']}
    #     return render_template('survey_result.html')
    # #return bl()

#@app.route('/result',methods=['GET''POST'])
#def result():
    #print request
    #return render_template('survey_result.html')



if __name__ == '__main__':
    app.run(debug=True, port=800)